package com.feng.sprider;

/**
 * 下载链接
 * 
 * @author feng
 *
 */
public class Threads implements Runnable {

	ImgUrlQuene imgUrl;
	UrlQuene urlQuene;
	VisitedUrlQuene visitedUrl;

	public Threads(UrlQuene urlQuene, VisitedUrlQuene visitedUrl,
			ImgUrlQuene imgUrl) {
		this.imgUrl = imgUrl;
		this.urlQuene = urlQuene;
		this.visitedUrl = visitedUrl;
	}

	Get get = new Get(urlQuene, visitedUrl, imgUrl);

	public synchronized void run() {
		while (true) {
			String url = null;
			if (imgUrl.getLength() < 50) {

				url = urlQuene.getUrl();

				System.out.println("Thread url " + url);

				
				//进入其中发生错误
				get.getUrls(url);
				
				/**
				 * 当获取完img时才能添加进访问过的队列 写在了获取图片方法中
				 */
				if (url != null) {
					get.getImgUrls(url);
				}
				
				// 放入已访问队列
				// 移出
				visitedUrl.addUrl(url);
				urlQuene.removeUrl(url);
			}

			try {
				// 5秒刷新
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}
